﻿using System;
using System.Linq.Expressions;

namespace HBKY.Shop.Common.Extension
{
    public static class QuoteDisplayExtension
    {
        /// <summary>
        /// 传入涨跌幅
        /// 传入"99.11",prefix="%"
        /// 返回格式：+99.11%
        /// </summary>
        /// <param name="rateStr"></param>
        /// <returns></returns>
        public static string ToQuoteRate(this string rateStr, string postfix = "%", decimal multiplication = 1,string prefix = "+")
        {
            if (string.IsNullOrWhiteSpace(rateStr))
            {
                return string.Empty;
            }

            decimal.TryParse(rateStr, out var rateDecimal);

            if (multiplication != 1)
            {
                rateDecimal = rateDecimal * multiplication;
            }
            rateDecimal = Math.Round(rateDecimal, 2);

            var op = rateDecimal > 0 ? prefix : string.Empty;

            return $"{op}{rateDecimal.ToString("f2")}{postfix}";
        }


        /// <summary>
        /// 传入涨跌幅
        /// 传入99.11D,prefix="%"
        /// 返回格式：+99.11%
        /// </summary>
        /// <param name="rateStr"></param>
        /// <returns></returns>
        public static string ToQouteRate(this decimal rate, string postfix = "%", decimal multiplication = 1)
        {
            return rate.ToString().ToQuoteRate(postfix, multiplication);
        }

        public static string ToQouteRate(this double rate, string postfix = "%", decimal multiplication = 1)
        {
            return rate.ToString().ToQuoteRate(postfix, multiplication);
        }

        public static string ToQouteRate(this double rate)
        {
            return rate.ToString().ToQuoteRate("%", 100,string.Empty);
        }

        public static string ToQouteRate(this decimal rate)
        {
            if (rate == default) return "--";
            return rate.ToString().ToQuoteRate("%", 1);
        }

        public static string ToPrefixQouteRate(this decimal rate)
        {
            if (rate == default) return "--";
            var str = rate.ToFixed2();
            return rate > 0 ? $"+{str}" : str;
        }
        public static string ToPrefixQouteRate(this decimal? rate)
        {
            if (rate == default) return "--";
            var str = rate.ToFixed2();
            return rate > 0 ? $"+{str}" : str;
        }

        public static string ToQouteRate(this decimal? rate)
        {
            if (rate == default) return "--";
            return rate.ToString().ToQuoteRate("%", 1);
        }


        public static string ToPercentage(this decimal? rate)
        {
            if (rate == default) return "--";
            return rate.ToString().ToQuoteRate("%", 100, "");
        }

        /// <summary>
        /// 传入涨跌幅
        /// 传入9 ,prefix="%"
        /// 返回格式："+9%"
        /// </summary>
        /// <param name="rateStr"></param>
        /// <returns></returns>
        public static string ToQouteRate(this int rate, string postfix = "%", decimal multiplication = 1)
        {
            return rate.ToString().ToQuoteRate(postfix, multiplication);
        }

        /// <summary>
        /// 基金规模
        /// </summary>
        public static string ToFundSizeDesc(this decimal? rate)
        {
            if (rate == default) return "--";
            return $"{rate}";  //亿元
        }

        /// <summary>
        /// 亿
        /// </summary>
        public static string ToUnitYI(this decimal rate)
        {
            if (rate == default) return "--";
            return $"{(rate / 100000000).ToString("0.00")}";
        }

        /// <summary>
        /// 亿
        /// </summary>
        public static string ToUnitYI(this decimal? rate)
        {
            if (rate == default) return "--";
            return $"{(rate.Value / 100000000).ToString("0.00")}";
        }

        /// <summary>
        /// 亿
        /// </summary>
        public static string ToUnitYI(this long rate)
        {
            if (rate == default) return "--";
            return $"{(decimal.Parse(rate.ToString()) / 100000000).ToString("0.00")}";
        }

        /// <summary>
        /// 万
        /// </summary>
        public static string ToUnitWan(this long rate)
        {
            if (rate == default) return "--";
            return $"{(decimal.Parse(rate.ToString()) / 10000).ToString("0.00")}";
        }

        /// <summary>
        /// 万
        /// </summary>
        public static string ToUnitWan(this long? rate)
        {
            if (rate == default) return "--";
            return $"{(decimal.Parse(rate.ToString()) / 10000).ToString("0.00")}";
        }

        /// <summary>
        /// 万
        /// </summary>
        public static string ToNullChar<TSource>(this TSource rate, string str)
        {
            if (rate == null) return "--";
            return str;
        }

        /// <summary>
        /// 亿元
        /// </summary>
        public static string To亿Desc(this decimal? rate)
        {
            if (rate == default) return "--";
            return $"{(rate.Value / 100000000).ToString("0.00")}亿手";
        }
    }
}
